package cn.xhjava.crm.workbench.service.impl;

import cn.xhjava.crm.commons.util.DateUtils;
import cn.xhjava.crm.commons.util.ExceptionUtil;
import cn.xhjava.crm.commons.util.UUIDUtils;
import cn.xhjava.crm.workbench.domain.Contacts;
import cn.xhjava.crm.workbench.domain.Customer;
import cn.xhjava.crm.workbench.domain.DateDataMapVO;
import cn.xhjava.crm.workbench.domain.FunnelVO;
import cn.xhjava.crm.workbench.mapper.ContactsMapper;
import cn.xhjava.crm.workbench.mapper.CustomerMapper;
import cn.xhjava.crm.workbench.service.ContactsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author Xiahu
 * @create 2023/7/12 0012
 */
@Service
public class ContactsServiceImpl implements ContactsService {
    @Autowired
    private ContactsMapper contactsMapper;

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public List<Contacts> selectForPage(Map<String, Object> map) throws Exception {
        return contactsMapper.queryContactsByConditionForPage(map);
    }

    @Override
    public int selectCountForPage(Map<String, Object> map) throws Exception {
        return contactsMapper.queryCountOfContactsByCondition(map);
    }

    @Override
    public int add(Contacts contacts) throws Exception {
        String customerName = contacts.getCustomerId();
        Customer customer = customerMapper.selectCustomerByName(customerName);
        if (customer == null) {
            customer = new Customer();
            customer.setId(UUIDUtils.getUUID());
            customer.setOwner(contacts.getOwner());
            customer.setCreateBy(contacts.getOwner());
            customer.setCreateTime(DateUtils.formateDateTime(new Date()));
            customer.setName(customerName);
            customerMapper.insert(customer);
        }
        contacts.setCustomerId(customer.getId());
        return contactsMapper.insert(contacts);
    }

    @Override
    public Contacts selectById(String id) throws Exception {
        return contactsMapper.selectById(id);
    }

    @Override
    public int update(Contacts contacts) throws Exception {
        // 根据contacts id获取客户
        Contacts dbContacts = contactsMapper.selectByPrimaryKey(contacts.getId());

        // 修改联系人参数
        Customer customer = customerMapper.selectByPrimaryKey(dbContacts.getCustomerId());
        if (customer != null) {
            customer.setName(contacts.getCustomerId());
            customer.setEditBy(contacts.getEditBy());
            customer.setEditTime(contacts.getEditTime());
            customerMapper.updateByPrimaryKeySelective(customer);
        }
        contacts.setCustomerId(customer.getId());
        return contactsMapper.updateByPrimaryKeySelective(contacts);
    }

    @Override
    public int delete(String[] ids) throws Exception {
        int res = 1;

        for (String id : ids) {
            contactsMapper.deleteByPrimaryKey(id);
        }

        return res;
    }

    @Override
    public Contacts selectDetailById(String id) throws Exception {
        return contactsMapper.selectDetailById(id);
    }

    @Override
    public List<Contacts> selectLikeName(String name) throws Exception {
        return contactsMapper.selectLikeName(name);
    }

    @Override
    public List<Contacts> selectContactsListByCustomerId(String id) throws Exception {
        return contactsMapper.selectContactsListByCustomerId(id);
    }

    @Override
    public int addByCustomerId(Contacts contacts) throws Exception {
        return contactsMapper.insert(contacts);
    }

    @Override
    public DateDataMapVO queryContactsCountGroupByCreateTime() throws Exception {
        List<FunnelVO> funnelVOList = contactsMapper.queryContactsCountGroupByCreateTime();
        DateDataMapVO res = new DateDataMapVO();
        LinkedList<String> date = new LinkedList();
        LinkedList<String> data = new LinkedList();
        for (FunnelVO vo : funnelVOList) {
            date.addLast(vo.getName());
            data.addLast(String.valueOf(vo.getValue()));
        }
        res.setDate(date);
        res.setData(data);
        return res;
    }

    @Override
    public List<FunnelVO> queryContactsCountGroupBySource() throws Exception {
        return contactsMapper.queryContactsCountGroupBySource();
    }

}
